#[derive(PartialOrd, Debug, PartialEq)]

struct Shoe {
    size: u32,
    style: String,
}

fn shoes_in_my_size(shoe: Vec<Shoe>, shoe_size: u32) -> Vec<Shoe>{
    shoe.into_iter().filter(|x| x.size == shoe_size).collect()
}

fn main() {

    let v1 = vec![1,2,3];

    let vi_iter = v1.iter();

    for x in vi_iter {
        println!("Got: {}", x);
    }

    let vi_iter = v1.iter();

    let total: i32 = vi_iter.sum();

    assert_eq!(total,6);

    let v1 = vec![1,2,3];

    let  v2: Vec<_> = v1.iter().map(|x| x+1).collect();

    assert_eq!(v2,vec![2,3,4]);
}

#[test]
fn filter_by_size() {
    let shoes = vec![
        Shoe {
            size: 10,
            style: String::from("sneaker"),
        },
        Shoe {
            size: 20,
            style: String::from("fake"),
        },
        Shoe {
            size: 10,
            style: String::from("boot"),
        },
    ];

    let in_my_size = shoes_in_my_size(shoes,10);

    assert_eq!(
        in_my_size,
        vec![
            Shoe {
                size: 10,
                style: String::from("sneaker")
            },
            Shoe {
                size: 10,
                style: String::from("boot")
            },
        ]
    );
}

struct Counter {
    count: u32,
}

impl Counter {
    fn new() -> Counter {
        Counter { count: 0}
    }
}

impl Iterator for Counter {
    type Item = u32;

    fn next(&mut self) -> Option<Self::Item> {
        if self.count < 5 {
            self.count += 1;
            Some(self.count)
        } else {
            None
        }
    }
}

#[test]
fn call_next_directly() {
    let mut counter = Counter::new();

    assert_eq!(counter.next(),Some(1));
    assert_eq!(counter.next(),Some(2));
    assert_eq!(counter.next(),Some(3));
    assert_eq!(counter.next(),Some(4));
    assert_eq!(counter.next(),Some(5));
    assert_eq!(counter.next(),None);
}

#[test]
fn using_other_iterator_trait_methods() {
    let sum: u32 = Counter::new()
        .zip(Counter::new().skip(1))
        .map(|(a,b)|  a * b)
        .filter(|x| x % 3 == 0)
        .sum();

    assert_eq!(18,sum);
}